package com.tanqidi.arcgisandroid.ui.projectConfig.exportData

import android.os.Bundle
import android.text.TextUtils
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ArrayAdapter
import androidx.fragment.app.activityViewModels
import androidx.fragment.app.viewModels
import androidx.navigation.fragment.findNavController
import androidx.navigation.fragment.navArgs
import com.esri.arcgisruntime.data.ArcGISFeatureTable
import com.esri.arcgisruntime.data.FeatureCollectionTable
import com.esri.arcgisruntime.data.FeatureTable
import com.tanqidi.arcgisandroid.R
import com.tanqidi.arcgisandroid.base.utils.getSPUtils
import com.tanqidi.arcgisandroid.base.utils.showToast
import com.tanqidi.arcgisandroid.data.layerConfig.LayerConfig
import com.tanqidi.arcgisandroid.databinding.FragmentExportDataBinding
import com.tanqidi.arcgisandroid.ui.HomeViewModel
import com.tanqidi.arcgisandroid.ui.projectConfig.exportData.ExportDataViewModel.Companion.EXPORT_TYPE_EXCEL
import com.tanqidi.arcgisandroid.ui.projectConfig.exportData.ExportDataViewModel.Companion.EXPORT_TYPE_GEODATABASE
import com.tanqidi.arcgisandroid.ui.projectConfig.exportData.ExportDataViewModel.Companion.EXPORT_TYPE_GEOJSON
import com.tanqidi.arcgisandroid.ui.projectConfig.exportData.ExportDataViewModel.Companion.EXPORT_TYPE_KML
import com.tanqidi.arcgisandroid.ui.projectConfig.exportData.ExportDataViewModel.Companion.EXPORT_TYPE_SHP
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
class ExportDataFragment : Fragment() {

    private val viewModel: ExportDataViewModel by viewModels()
    private val homeViewModel by activityViewModels<HomeViewModel>()
    private lateinit var binding: FragmentExportDataBinding
    private var type:String? = null
    private var featureTable: FeatureTable? = null
    private var layerName:String? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = FragmentExportDataBinding.inflate(layoutInflater)
    }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        initTopBar()
        initViewEvent()
        initLiveData()
        return binding.root
    }

    private fun initLiveData() {
        viewModel.downStatusLiveData.observe(viewLifecycleOwner) {
            binding.progressBar.hide()
        }
    }

    private fun initViewEvent() {
        val layerNameList = homeViewModel.featureTable.keys.toTypedArray()
        binding.layers.apply {
            setAdapter(getArrayAdapter(layerNameList))
            setOnItemClickListener { parent, view, position, id ->
                layerName = layerNameList[position]
                featureTable = homeViewModel.featureTable[layerName]
            }
        }

        val typeNameList = listOf(EXPORT_TYPE_KML).toTypedArray()
        binding.type.apply {
            setAdapter(getArrayAdapter(typeNameList))
            setOnItemClickListener { parent, view, position, id ->
                type = typeNameList[position]
            }
        }

        binding.export.setOnClickListener {
            if(featureTable == null){
                showToast("请选择图层")
                return@setOnClickListener
            }
            if(type == null || TextUtils.isEmpty(type)){
                showToast("请选择类型")
                return@setOnClickListener
            }
            binding.progressBar.show()
            viewModel.export(featureTable!!, layerName!!, type!!)
        }
    }

    private fun getArrayAdapter(list: Array<String>): ArrayAdapter<String> {
        return ArrayAdapter(requireContext(), R.layout.cat_exposed_dropdown_popup_item, list)
    }

    private fun initTopBar() {
        binding.topAppBar.setNavigationOnClickListener {
            findNavController().navigateUp()
        }
    }
}